package patterns

import (
	"fmt"
	"net/http"
	"time"
)

type Result struct {
	Error    error
	Response *http.Response
}

func TryErrorHandling() {

	checkStatus := func(done <-chan interface{}, urls ...string) <-chan Result {
		results := make(chan Result)
		go func() {
			defer close(results)
			for _, url := range urls {
				resp, err := http.Get(url)
				result := Result{Error: err, Response: resp}
				select {
				case <-done:
					return
				case results <- result:
				}
			}
		}()
		return results
	}
	done := make(chan interface{})
	go func() {
		defer close(done)
		time.Sleep(1 * time.Second)
	}()
	url := []string{"https://www.baidu.com", "https://www.abc.ddd"}
	for result := range checkStatus(done, url...) {
		if result.Error != nil {
			fmt.Println("error: ", result.Error)
			continue
		}
		fmt.Println("Response: ", result.Response.Status)
	}

}
